﻿using System;
using System.Text.RegularExpressions;
using WorkItemAnalyser.Core.Assertions;

namespace WorkItemAnalyser.Core.Parsers
{
    public class TextFieldAssertionParser : IAssertionParser
    {
        const string FieldAssertionGrammer = @"the (?<field>.*) should be (?<value>.*)";
        
        public IAssertion ParseAssertion(string assertionText)
        {
            if (string.IsNullOrWhiteSpace(assertionText))
                return null;
            
            var ruleExpression = new Regex(FieldAssertionGrammer);
            var matches = ruleExpression.Match(assertionText);

            var fieldName = matches.Groups["field"].ToString();
            var value = matches.Groups["value"].ToString();

            if (string.IsNullOrWhiteSpace(fieldName) || (string.IsNullOrWhiteSpace(value)))
                return null;

            return new FieldAssertion(fieldName, value);
        }
    }
}
